package com.atguigu.tingshu.user.pattern.factory;

import com.atguigu.tingshu.user.pattern.DeliveryStrategy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * @author: atguigu
 * @create: 2025-08-04 09:27
 */
@Slf4j
@Component
public class DeliveryStrategyFactory {

    //自动发现策略接口下所有实现类对象，注入到IOC容器中Map中

    /**
     * 自动发现 策略接口DeliveryStrategy下所有实现类对象 注入Map中
     * Map的Key是策略实现类对象BeanID
     * Map的Value是策略实现类对象
     */
    @Autowired
    private Map<String, DeliveryStrategy> deliveryStrategyMap;

    @Autowired
    private List<DeliveryStrategy> deliveryStrategyList;


    /**
     * 根据商品类型获取对应的策略实现类对象
     * @param itemType
     * @return
     */
    public DeliveryStrategy getDeliveryStrategy(String itemType) {
        log.info("itemType:{}", itemType);
        if (deliveryStrategyMap.containsKey(itemType)) {
            return deliveryStrategyMap.get(itemType);
        }
        log.error("当前商品类型不支持处理！");
        throw new RuntimeException("当前商品类型不支持处理！");
    }
}
